This article provides a complete overview of the various data types you can use with your queries to retrieve, modify, and sync data in Ditto. In addition, you’ll find an introduction to related concepts like conflict resolution strategies.
CREATE
, READ
, UPDATE
, and DELETE
(CRUD) database operations and data sync.
store
namespace. Once executed, Ditto returns a response object encapsulating the results.&
The following table provides a high-level overview of the different ways you can perform CRUD in Ditto:
Operation | Description |
---|---|
CREATE | Using the INSERT statement, either insert a new document or update an existing document for a given document ID. |
READ | Using the SELECT statement, retrieve documents based on the specific criteria you pass as parameters in your function. |
UPDATE | Using the UPDATE method, write changes to Ditto. |
DELETE | Using the EVICT method, remove local data from your store. In addition, using a soft-delete pattern, indicate data as deleted without physically removing it from Ditto. |
Item | Description |
---|---|
1 | The name identifying the data. |
2 | The value that holds the actual data to store. |
location
field property is logically grouped with details about the car, such as its make
, year
, and color
.&
It contains nested fields. also referred to as subfields,* *representing both the coordinates
and the address
where the car is located:
CREATE
, READ
, UPDATE
, and DELETE
(CRUD) and filter data for sync subscriptions.
DQL is a familiar SQL‑like syntax designed specifically for Ditto’s edge-sync features that enable the platform’s offline-first capabilities. DQL offers features like:
ditto.store.execute
— Interact with data stored locally within the Small Peer device running your app. (See ditto.store.execute)ditto.store.registerObserver
— Set up store listeners to monitor changes to data in the local Ditto store based on specified query criteria. (See ditto.store.registerObserver)ditto.sync.registerSubscription
— Set up subscriptions for syncing data from other peers based on their associated queries. (See ditto.sync.registerSubscription)ditto.presence
— Enable end-user functionality like mesh network monitoring, management, and transport optimization. (See Presence Operations)Scalar type
string
, boolean
, array
, and any other basic primitive type.&In addition, a scalar type can be a JSON blob
object capable of nesting multiple key-value pairs functioning as a single unit.&Data type
REGISTER
, MAP
, and ATTACHMENT
.&As illustrated on the right, each data type includes two components:string
, boolean
, and so on.&
REGISTER
; you’ll use other data types in specific scenarios where appropriate.REGISTER
— the default data type in Ditto — you must explicitly specify the type in your query; otherwise, Ditto defaults to the REGISTER
type as follows.&
SELECT
statement query explicitly expressed as a MAP
structure. It specifies retrieval of the MAP
structure storing the"features"
collection with a "trim"
field property set to a value of "standard"
:
REGISTER
, MAP
, and ATTACHMENT
— adhere to the causal consistency model when resolving concurrency conflicts.&
The causal consistency model is a guarantee that if there is an operation that must happen before another operation — for example, events A and B, where B is a result of A — all peers agree upon and observe the same sequential order of these operations; as in, A always executes before B.
Type | Merge Strategy | Description | Use Case |
---|---|---|---|
REGISTER | ”Last write wins” | Stores a single value and allows for concurrent updates. | Store JSON‑compatible scalar subtypes, including a nested blob representing two or more fields as a single object. |
MAP | ”Add wins” | Contains a nested object consisting of any Ditto type: REGISTER , MAP , and ATTACHMENT . | Enable field-level concurrent hierarchy within a Ditto document. |
ATTACHMENT | ”Last write wins” | Stores the token you use to retrieve the ATTACHMENT . | Reduce Small Peer resource usage by storing data that can be retrieved lazily; as in, you fetch the data only when needed. |
REGISTER
MAP
MAP
, a JSON object functions as a single unit. Managing both the coordinate and address as a cohesive unit ensures that any changes made to one automatically update the other.
array
type in Ditto acts as a REGISTER
and therefore encapsulating values function as a single unit.&Representing coordinates
as an array
, as demonstrated in the previous snippet, is the smart choice since its latitude and longitude values function as a unified entity, always changing simultaneously.®ISTER
fields in a MAP
:
MAP
within another MAP
, as follows:&
MAPS
in a single document or opt for a flat model depends on requirements, relationships between data, and tolerance for certain tradeoffs.REGISTER
data type functions as the default in Ditto for scalar-encoded values. So any scalar-encoded values, including embedded JSON objects and arrays
, are assumed to be type REGISTER
.Item | Type | Description |
---|---|---|
1 | REGISTER | A value can be any JSON-encoded primitive type: boolean , numeric , binary , string , array , andNULL . |
2 | REGISTER | A hierarchical data structure of multiple JSON-encoded fields nested within a larger JSON object and serves as a single value. |
3 | MAP | A hierarchical data structure of two or more key-value pairs encoded using any data type — REGISTER , ATTACHMENT , or MAP . |
4 | Response Object | The response object returned after creating a new ATTACHMENT .You use the ATTACHMENT response object within your app’s code to retrieve and display the file to the end user, as appropriate, and to update or delete the file. |
5 | Attachment Token | The pointer that Ditto uses to reference the large file’s storage location when fetching. You can use an ATTACHMENT for any file type, including binary data of 50 megapixels or more, such as an mp4 file, or a large document object featuring complex hierarchical structures. |
MAP
data types is the possibility for two offline peers to create a new document, in which one peer represents the field as an object (MAP
), while the other peer represents the field as an array
.
MAP
types.&
Peer A creates the following new document:
create
, read
, update
, delete
(CRUD) operation against the store
namespace.
store
namespace.
execute
API method and a local SELECT
query to search within the local Small Peer store for data in a dataset named cars
:&
INSERT
. Following is an example of how to perform an INSERT
operation using Ditto’s SDK.
execute
API method on a SELECT
query as follows:
UPDATE
operation within the cars
collection, changing the color to 'blue'
and the mileage to 3001
in documents where the _id
field is '123'
, as follows:
Evict
method to clear one or more documents from the local Ditto store. Once invoked, the documents are no longer accessible by local queries, however, they remain accessible from other peers connected in the mesh.&
The following snippet shows how to write a basic EVICT
operation to purge the document with an _id
field of '123'
from the local Ditto store:&
MAP
to sync concurrent changes.&
Understanding these patterns will help you make informed design decisions in your app.&
ATTACHMENT
.&
With the ATTACHMENT
data type, you can implement lazy loading. Lazy loading is when you delay retrieval until necessary rather than aggressively fetching the data in anticipation of hypothetical future use. This “on-demand” retrieval pattern enhances performance by optimizing resource usage.&
color:red
to color:blue
:
mileage
field:&
increment
of 200
and the color
change to blue
:
Pattern | Risks |
---|---|
Lazy-load retrieval | - Increased latency if resources are not efficiently preloaded |
MAP
for sync | Race conditions causing data inconsistency |